Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.4K
Visualizações
ERROR NullInjectorError: R3InjectorError(DynamicTestModule)[KDSDialogService -> MatDialog -> MatDialog]: NullInjectorError: No provider for MatDialog

I'm doing unit testing on my application. I'm new to testing, so I need your help.

In my application I make a service that uses MatDialog ( KDSDialogService). I've tried putting many import alternatives, my service or matdialog as providers I have no idea what to do

export declare class KDSDialogService {
    dialog: MatDialog;
    private dialogRef;
    constructor(dialog: MatDialog);
    open(componentOrTemplateRef: ComponentType<any> | TemplateRef<any>, title?: string, data?: any, size?: DialogSize, showClose?: boolean): MatDialogRef<any, any>;
    static ɵfac: ɵngcc0.ɵɵFactoryDef<KDSDialogService, never>;
}

And in my home.component.spec I import and make the declarations here, but I still get this error.

describe('HomeComponent', () => {
   let component: HomeComponent;
   let fixture: ComponentFixture<HomeComponent>;

   beforeEach(async(() => {
      TestBed.configureTestingModule({
         declarations: [HomeComponent ],
         imports:[KDSDialogService, MatDialogModule],
  
      }).compileComponents();
   }));

   beforeEach(() => {
      fixture = TestBed.createComponent(HomeComponent);
      component = fixture.componentInstance;
    
   });

   it('should create', () => {
      fixture.detectChanges();
      expect(component).toBeTruthy();
   });
});

Print of the error

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can mock it like this:

import { MatDialog } from '@angular/material/dialog';
//...
    let matDialogService: jasmine.SpyObj<MatDialog>;
    matDialogService = jasmine.createSpyObj<MatDialog>('MatDialog', ['open']);

    TestBed.configureTestingModule({
      providers: [
        {
          provide: MatDialog,
          useValue: matDialogService,
        },

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda